﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TAB50RS
    {
         
        public TAB50RS()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MESSAGES += "RSABPNEW;";
            InitializeComponent();
            POSTYP = "YD ";
        } 
        #region====WorkFlow========
        MessageInfo Lastmsg
        {
            get
            {
                return TMV.Lastmsg;
            }
            set
            {
                TMV.Lastmsg = value;
            }
        }
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            IsBusy = true;
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "RSABPNEW":
                    if (Lastmsg != null)
                    {
                        MessageBox.Show("请先完成前一个订单创建");
                        //            return;
                    }
                    RULEKEY = "";//先装载规则
                    RULEKEY = msg.ORDER;
                    Lastmsg = msg;
                    ARTST = msg.WAKZ;
                    ART = msg.ART;
                    VAR = msg.VAR;
                    FARBE = msg.FARBE;
                    PRART = ART + VAR + FARBE;
                    PM["ORDER"] = msg.ORDER;
                    PM["WAKZ"] = msg.WAKZ;
                    PM["ART"] = msg.ART;
                    PM["VAR"] = msg.VAR;
                    PM["FARBE"] = msg.FARBE;
                    PM["BESCHICH"] = msg.BESCHICH.PadRight(10);
                    PM["DESS"] = msg.DESS.PadRight(10);
                    PM["COLORIT"] = msg.COLORIT.PadRight(10);
                    PM["GROE"] = msg.COLORIT.PadRight(7);
                    TMV.RSABPNEW(PM);
                    ORDERNUM = msg.ORDER;
                    IsShowAB = false;
                    IsShowAB = true;
                    break;
            }

            IsBusy = false;
        }
        #endregion

        public virtual void btnPrePlan_Click(object sender, RoutedEventArgs e)
        {
            if (dwAB == null)
                return;
            if (Lastmsg == null)
            {
                Lastmsg = new MessageInfo();
                Lastmsg.ORDER = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString();
                Lastmsg.ART = dwAB["ABPART"].ToString();
                Lastmsg.VAR = dwAB["ABPVAR"].ToString();
                Lastmsg.FARBE = dwAB["ABPFARBE"].ToString();
            }
            PM["ORDER"] = Lastmsg.ORDER;
            PM["WAKZ"] = Lastmsg.WAKZ;
            PM["ART"] = Lastmsg.ART;
            PM["VAR"] = Lastmsg.VAR;
            PM["BESCHICH"] = Lastmsg.BESCHICH.PadRight(10);
            PM["DESS"] = Lastmsg.DESS.PadRight(10);
            PM["COLORIT"] = Lastmsg.COLORIT.PadRight(10);
            PM["GROE"] = Lastmsg.COLORIT.PadRight(7);
            TMV.RSABPNEW(PM);
            if (dwAB != null)
            {
                string key = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
                ORDERNUM = "";
                ORDERNUM = key;
            }

        }

        private void btnSplitBatch_Click(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.AB50SPLIT(RUNID, KEY);
        }

        private void btnRepaire_Click(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.AB50RVREP(RUNID, KEY);
        }

        private void btnGotoCombine_Click(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.AB50COMBINE(RUNID, KEY);
        }

        public void FBApproveCommand(object sender, RoutedEventArgs e)
        {
            if (dwAB == null)
                return;
            if (dwRule == null)
                return;
            PM["ORDERNUM"] = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
            VABPOS.Instance.ENDABP(PM);
            TMV.VABF.ENDABF(PM);

            string order = dwRule["P5"].ToString();
            DataRow dwVKP = TMV.VVKP.TBMain.NewRow();
            dwVKP["VKPMANDANT"] = app.Mandant;
            dwVKP["VKPAUFKRS"] = order.Substring(0, 2);
            dwVKP["VKPAUFLFD"] = order.Substring(2, 6);
            dwVKP["VKPAUFPOS"] = order.Substring(8, 3);
            dwVKP["VKPEXAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
            dwVKP["VKPEXAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
            dwVKP["VKPEXAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
            dwVKP["VKPEXART"] = Helpers.Helper.ConvertGB2312To8859P1("复板确认");
            TMV.VVKP.UpdateVKPOS(dwVKP, app.company, 11);
            PM["RUNID"] = this.RUNID;
            PM["VKPAUF"] = order;
            PM["VKPEXART"] = dwVKP["VKPEXART"].ToString();
            TMV.VVKP.VKPCHK(PM);
        }
        public void FBStartCommand(object sender, RoutedEventArgs e)
        {
            if (dwAB == null)
                return;
            if (dwRule == null)
                return;

            PM["ORDERNUM"] = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
            VABPOS.Instance.ENDABP(PM);
            VABPOS.Instance.REACTIVE(PM);
            string order = dwRule["P5"].ToString();
            DataRow dwVKP = TMV.VVKP.TBMain.NewRow();
            dwVKP["VKPMANDANT"] = app.Mandant;
            dwVKP["VKPAUFKRS"] = order.Substring(0, 2);
            dwVKP["VKPAUFLFD"] = order.Substring(2, 6);
            dwVKP["VKPAUFPOS"] = order.Substring(8, 3);
            dwVKP["VKPEXAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
            dwVKP["VKPEXAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
            dwVKP["VKPEXAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
            dwVKP["VKPEXART"] = Helpers.Helper.ConvertGB2312To8859P1("启动复板");
            TMV.VVKP.UpdateVKPOS(dwVKP, app.company, 11);
            PM["RUNID"] = this.RUNID;
            PM["VKPAUF"] = order;
            PM["VKPEXART"] = dwVKP["VKPEXART"].ToString();
            TMV.VVKP.VKPCHK(PM);
        }

        private void FBResetCommand(object sender, RoutedEventArgs e)
        {
            PM["ORDERNUM"] = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
            VABPOS.Instance.REACTIVE(PM);
            string order = dwRule["P5"].ToString();
            DataRow dwVKP = TMV.VVKP.TBMain.NewRow();
            dwVKP["VKPMANDANT"] = app.Mandant;
            dwVKP["VKPAUFKRS"] = order.Substring(0, 2);
            dwVKP["VKPAUFLFD"] = order.Substring(2, 6);
            dwVKP["VKPAUFPOS"] = order.Substring(8, 3);
            dwVKP["VKPEXAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
            dwVKP["VKPEXAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
            dwVKP["VKPEXAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
            dwVKP["VKPEXART"] = Helpers.Helper.ConvertGB2312To8859P1("启动复板");
            TMV.VVKP.UpdateVKPOS(dwVKP, app.company, 11);
            PM["RUNID"] = this.RUNID;
            PM["VKPAUF"] = order;
            PM["VKPEXART"] = dwVKP["VKPEXART"].ToString();
            TMV.VVKP.VKPCHK(PM);
        }

    }
    public partial class VABPOS
    {
        public void RSABPNEW(Dictionary<string, object> pm)
        {
            try
            {

                if (dwRule["UNDES"].ToString().Trim().Length < 8)
                    return;
                if (PRLO.Length < 2)
                    return;
                if (PRLB.Length < 2)
                    return;
                APZANRKRS = dwRule["UNDES"].ToString().Substring(0, 2);
                APZALFDNR = dwRule["UNDES"].ToString().Substring(2, 6);
                if (APZALFDNR.Trim().Length < 6)
                {
                    throw new Exception("请选择流程");
                }
                //QTYMAT = (QTYEND - F02 + F01) * R01;
                //QTYMAT = VKPQTY * R01;
                double TargetRatio = QTYMAT / QTYEND;//例如1.2，就是100kg成品需要120kg原料
                if (TargetRatio < 0)
                    TargetRatio = 1;

                #region=======dwData==========
                DataRow dwABP = TBMain.NewRow();
                SetUpNewRow(ref dwABP, null);
                ShareedABP(dwABP, pm);
                dwABP["ABPZLO"] = PRLO;
                dwABP["ABPZLB"] = PRLB;
                dwABP["ABPSME"] = "5";
                dwABP["ABPPOSTYP"] = POSTYP;
                dwABP["ABPWAKZ"] = "G";
                dwABP["ABPAUFART"] = "GP";
                dwABP["ABPSOLLMENGE1"] = 0;
                dwABP["ABPSOLLMENGE2"] = 0;
                dwABP["ABPSOLLMENGE3"] = 0;
                dwABP["ABPSOLLMENGE4"] = 0;
                dwABP["ABPSOLLMENGE5"] = QTYEND * 100;
                dwABP["ABPSOLLMENGE6"] = 0;
                dwABP["ABPSOLLMENGE7"] = System.Convert.ToDouble(dwRule["VKPQTY"].ToString()) * 100;//在YGR需要记录销售单数量
                dwABP["ABPSOLLMENGE8"] = 0;
                dwABP["ABPZUGKG"] = 0;
                dwABP["ABPSTKLG"] = Math.Round(TargetRatio * 10000000);
                #endregion

                Insert(dwABP);

                VPR.UpdateICC_PRULES(dwRule, 1);
                PM["KRS"] = KRS;
                //PM["NEWABK"] = msg.P3;
                PM["VKAUF"] = dwRule["P4"];
                //PM["VKAUF"] = msg.KRS + msg.LFD + msg.POS + msg.P1 + msg.AUFTYP;
                PreparePM(dwABP);

                VGD.NEWGD310(PM);
                PM["ABAAUFTYP"] = "V";
                VMU.MUNEW(PM);

                VPAR.NABPR(PM);

                VABF.ABFNEW(PM);

                VGD.NEWGD713(PM); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.Message);
            }
        }
    }
}
